package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.ScalarException;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.utils.Logger;

import java.util.HashMap;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 18, 2005
 * Time: 4:41:41 PM
 */
public class ShippingRequestConfirmCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/shippingRequest.do";
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "search" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        DBTransaction dbTransaction                 = null;
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();
        boolean error                               = false;
        boolean isNewShipRequest                    = (shippingRequest.getShipRequestId() == -1 );
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }

        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            ShippingRequestDAO.saveShipRequestDetails( dbTransaction , shippingRequest, user.getUserId() );
            ShippingRequestDAO.saveShipRequestLotDetails( dbTransaction, shippingRequest );           

        }
        catch( Exception e )
        {
            e.printStackTrace();
            error = true;
            errors.put( "Could not save the Shipping Request", null );
        }

        finally
        {
            if( dbTransaction != null )
            {
                if( error == false )
                {
                    try
                    {
                        dbTransaction.commit();
                        if ( isNewShipRequest ) //SEND MAIL ONLY WHEN CREATING NEW SHIPPING REQUEST
                        {
                            //TO LOAD THE SAVED SHIPPING REQUEST INFO
                            ShippingRequest savedShippingRequest  = ShippingRequestDAO.getShippingRequest( null, shippingRequest.getShipRequestId() );
                            try
                            {
                                //TO SEND THE MAIL ALERTS TO VENDOR CONTACT PERSONS
                                ShippingRequestDAO.sendShipRequestMail( savedShippingRequest, user.getEmail() );
                            }
                            catch( Exception e )
                            {
                                e.printStackTrace();
                                errors.put( "Could not send Shipping Request Email", null );
                            }
                        }
                    }
                    catch( Exception e )
                    {
                        error = true;
                        e.printStackTrace();
                        Logger.debug( "Error while committing the database update" );
                    }
                }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                    }
                    catch ( Exception e )
                    {
                        error = true;
                        Logger.debug( "Error while rollback the database update" );
                    }
                }
            }
        }
        if( error == false )
        {
            info.put( "Shipping Request Saved Successfully",null );
            info.put ( "View/Print ,Click here to <a href='javascript:printView("+shippingRequest.getShipRequestId() +");' onmouseover=\"window.status='Click here to View/print Ship Request';return true\" onmouseout=\"window.status='';return true\"> view/print </a>Ship Request Details", null );
        }
        shippingRequestForm.setShippingRequest( new ShippingRequest() );
        //forwarding to the search page
        request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
        request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
        request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
        request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
        actionForward  = mapping.findForward( "search" );
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );        
        return actionForward;
    }

}
